package com.example.mainstream.fragment;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.CompoundButton;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.example.shopapp.App;
import com.example.shopapp.R;
import com.example.shopapp.adapter.OnShopCarListener;
import com.example.shopapp.adapter.ShopCarAdapter;
import com.example.shopapp.bean.Order;
import com.example.shopapp.bean.OrderBean;
import com.example.shopapp.bean.ShopAddress;
import com.example.shopapp.database.OrderBeanDBUtils;
import com.example.shopapp.database.ShopAddressDBUtils;
import com.example.shopapp.databinding.FragmentTabCarBinding;
import com.example.shopapp.ui.dialog.OnPayLinstener;
import com.example.shopapp.ui.dialog.PayTypesDialog;
import com.example.shopapp.ui.user.ShopAddressListActivity;
import com.example.shopapp.utils.NotificationHelper;
import com.example.shopapp.utils.Utils;
import com.example.shopapp.widget.MyItemDecoration;
import com.hb.dialog.myDialog.MyPayInputDialog;
import com.kongzue.dialogx.dialogs.MessageDialog;
import com.kongzue.dialogx.interfaces.OnDialogButtonClickListener;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import cn.project.base.baseui.BaseNormalFragment;

public class ShopCarFragment extends BaseNormalFragment<FragmentTabCarBinding> implements OnShopCarListener, OnPayLinstener {

    private ShopCarAdapter shopCarAdapter;
    String address_id = "";
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_tab_car;
    }

    @Override
    protected void initData() {
        shopCarAdapter = new ShopCarAdapter(getActivity(), this);
        mBinding.rvShop.setLayoutManager(new LinearLayoutManager(getActivity()));
        mBinding.rvShop.addItemDecoration(new MyItemDecoration(1));
        mBinding.rvShop.setAdapter(shopCarAdapter);
        shopCarAdapter.setNewData(Utils.orderList);
        shopCarAdapter.notifyDataSetChanged();
        double money = 0;
        if (Utils.orderList.size() > 0) {
            for (int i = 0; i < Utils.orderList.size(); i++) {
                if(Utils.orderList.get(i).isIsselect()){
                    money += Utils.orderList.get(i).getCount() * Double.parseDouble(Utils.orderList.get(i).getPrice());
                }
            }
        }
        mBinding.tvMoney.setText(money + " 元");


        //去结算
       mBinding.jiesuan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(Utils.orderList.size() == 0){
                    showToast("请先购买商品");
                    return;
                }else {
                    List<ShopAddress> shopAddressList = ShopAddressDBUtils.getInstance(getActivity()).getListShopAddress(App.getInstance().user.getUser_id());
                    if(shopAddressList.size() == 0){//判断是否有收获地址
                        addShopAddress();
                    }else {
                        boolean isDefault = false;
                        for (int i = 0; i < shopAddressList.size(); i++) {
                            if("0".equals(shopAddressList.get(i).getDefaultcity())){
                                address_id = shopAddressList.get(i).getAddress_id();
                                isDefault = true;
                            }
                        }
                        if (isDefault){
                            choosePayWays();
                        }else {
                            addShopAddress();
                        }

                    }
                }
            }
        });
        //是否全选
        mBinding.spcCbAll.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {
                    double money = 0;
                    if (Utils.orderList.size() > 0) {
                        for (int i = 0; i < Utils.orderList.size(); i++) {
                            money += Utils.orderList.get(i).getCount() * Double.parseDouble(Utils.orderList.get(i).getPrice());
                            Utils.orderList.get(i).setIsselect(true);
                        }
                    }
                    mBinding.tvMoney.setText(money + " 元");
                } else {
                    if (Utils.orderList.size() > 0) {
                        for (int i = 0; i < Utils.orderList.size(); i++) {
                            Utils.orderList.get(i).setIsselect(false);
                        }
                    }
                    mBinding.tvMoney.setText("0 元");
                }
                shopCarAdapter.notifyDataSetChanged();
            }
        });
    }

    private void choosePayWays() {
        PayTypesDialog payTypesDialog = new PayTypesDialog(getActivity(),R.style.pay_type_dialog,this);
        payTypesDialog.show();
    }

    private void addShopAddress() {
        MessageDialog.build()
                .setTitle("提示")
                .setMessage("还未设置收货地址，请先添加收货地址")
                .setOkButton("确定", new OnDialogButtonClickListener<MessageDialog>() {
                    @Override
                    public boolean onClick(MessageDialog dialog, View v) {
                        startActivity(new Intent(getActivity(), ShopAddressListActivity.class));
                        return false;
                    }
                })
                .setCancelButton("取消")
                .show();
    }

    @Override
    public void onPayClick() {
      jisuan();
    }

    @Override
    public void onLongClick(Order order) {
        MessageDialog.build()
                .setTitle("提示")
                .setMessage("是否取消购买")
                .setOkButton("确定", new OnDialogButtonClickListener<MessageDialog>() {
                    @Override
                    public boolean onClick(MessageDialog dialog, View v) {
                        Utils.orderList.remove(order);
                        jisuan();
                        return false;
                    }
                })
                .setCancelButton("取消")
                .show();
    }

    @Override
    public void onEventMainThread(String event) {
        super.onEventMainThread(event);
        if ("购物车".equals(event)){
            jisuan();
        }
    }

    private void jisuan(){
        double money = 0;
        if (Utils.orderList.size() > 0) {
            for (int i = 0; i < Utils.orderList.size(); i++) {
                if(Utils.orderList.get(i).isIsselect()){
                    Log.e("输出数据",Utils.orderList.get(i).getCount()+"");
                    money += Utils.orderList.get(i).getCount() * Double.parseDouble(Utils.orderList.get(i).getPrice());
                }
            }
        }
        NotificationHelper.show(getActivity(),"您的商品已购买成功！！！");
        mBinding.tvMoney.setText(money + " 元");
        shopCarAdapter.setNewData(Utils.orderList);
        shopCarAdapter.notifyDataSetChanged();
    }

    @Override
    public void onPayClick(int type) {
        switch (type){
            case 1:
                pay("支付宝支付");
                break;
            case 2:
                pay("微信支付");
                break;
            default:
                break;
        }
    }
    long SysId = System.currentTimeMillis();
    private void pay(String msg){
        final MyPayInputDialog myPayInputDialog = new MyPayInputDialog(getActivity()).Builder();
        myPayInputDialog.setResultListener(new MyPayInputDialog.ResultListener() {
            @Override
            public void onResult(String result) {

                for (int i = 0; i < Utils.orderList.size(); i++) {
                    if (Utils.orderList.get(i).isIsselect()){
                        OrderBean orderBean = new OrderBean();
                        orderBean.setUrl(Utils.orderList.get(i).getUrl());
                        orderBean.setGoods_type(Utils.orderList.get(i).getGoods_type());
                        orderBean.setState("0");
                        orderBean.setPrice(Utils.orderList.get(i).getPrice());
                        orderBean.setTitle(Utils.orderList.get(i).getTitle());
                        orderBean.setCount(Utils.orderList.get(i).getCount()+"");
                        orderBean.setAddress_id(address_id);
                        orderBean.setDetail_address(Utils.orderList.get(i).getDetail_address());
                        orderBean.setAddress(Utils.orderList.get(i).getAddress());
                        orderBean.setMerchatid(Utils.orderList.get(i).getMerchatId());
                        orderBean.setUser_id(App.getInstance().user.getUser_id());
                        orderBean.setContent(Utils.orderList.get(i).getContent());
                        OrderBeanDBUtils.getInstance(getActivity()).insert(orderBean);
                    }

                }
                showToast("支付成功");
                Utils.orderList.clear();
                EventBus.getDefault().postSticky("已购买");
                shopCarAdapter.setNewData(Utils.orderList);
                shopCarAdapter.notifyDataSetChanged();
                mBinding.tvMoney.setText("0 元");
                address_id = "";
                myPayInputDialog.dismiss();

                myPayInputDialog.dismiss();
            }
        }).setTitle(msg);
        myPayInputDialog.show();


    }
}
